package cn.zxh.csdn_maven.work.flow.login.chains;

import cn.zxh.csdn_maven.base.pojo.Cookies;
import cn.zxh.csdn_maven.base.util.Random4;
import cn.zxh.csdn_maven.work.flow.login.AbstractFlowController;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class LoginCookieFlow extends AbstractFlowController {

    @Override
    public Map<String, Object> execute(HttpClient client, Map<String, Object> params) throws ClientProtocolException, IOException {
        Map<String, Object> result = new HashMap<String, Object>();
        String url = getBaseUrl()+"?"+ Random4.getData();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = client.execute(httpGet);
        if(response.getStatusLine().toString().indexOf("200")!=-1){
            result.put("msg", "success");
        }
        httpGet.releaseConnection();
        /** 第一时间设置cookie */
        Cookies.setCookies(response.getHeaders("Set-Cookie"));
        result.put("msg", "success");
        return result;
    }

    @Override
    public String getBaseUrl() {
        return "https://passport.csdn.net/account/login";
    }
}
